package ms.authority.service;

import com.alibaba.fastjson.JSONObject;
import ms.authority.entity.Permission;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Project Name: ms
 * File Name: PermissionService
 * Package Name: ms.authority.service
 * Date: 2018/5/7  14:52
 * Copyright (c) 2018, tianyul All Rights Reserved.
 */
@Service
public class PermissionService {

    /**
     * 获取权限列表
     * @return
     */
    @Cacheable(value = "permission")
    public List<Permission> getList() throws IOException {
        List<Permission> permissions = new ArrayList<>();
        File file = ResourceUtils.getFile("classpath:permission.properties");
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));

        String string = "";
        String jsonString = null;

        while((jsonString = br.readLine()) != null){
            string += jsonString;
        }

        return (List<Permission>) JSONObject.parse(string);
    }

    /**
     * 清空缓存
     */
    @CacheEvict(value= {"permission"},allEntries=true)
    public void clean() {
    }
}
